﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using FarseerGames.FarseerPhysics.Collisions;
using UnitLibrary.Managers;

namespace UnitLibrary.GameObjects.Units
{
    public delegate void OnShoot(Vector2 startLocation, Vector2 endLocation);

    public class Bullet: UnitGen
    {
        public Bullet()
        {
            Shoot += Fire;
        }

        public event OnShoot Shoot;

        private bool _isRemoveOnContact = true;

        private int _numberOfBounces = 5;

        private BulletM _bulletManager;

        [ContentSerializer]
        /// <summary>
        /// 
        /// </summary>
        public bool IsRemoveOnContact
        {
            get { return _isRemoveOnContact; }
            set { _isRemoveOnContact = value; }
        }

        [ContentSerializer]
        /// <summary>
        /// 
        /// </summary>
        public int NumberOfBounces
        {
            get { return _numberOfBounces; }
            set { _numberOfBounces = value; }
        }

        public bool OnCollision(Geom geom1, Geom geom2, ContactList contactlist)
        {
            Iid g1tag = (Iid)geom1.Tag;
            Iid g2tag = (Iid)geom2.Tag;
            //Geom 2 tag was never created, we don't know the type, so just return true

            if (geom1.Tag == null)
            {
                Console.WriteLine("Geom 1 tag inside of Bullet.cs OnCollision was never instantiated");
                //Return true since we don't know what the geoms are
                return true;
            }
            if (geom2.Tag == null)
            {
                Console.WriteLine("Geom 2 tag inside of Bullet.cs OnCollision was never instantiated");
                //Return true since we don't know what the geoms are
                return true;
            }
            if (g1tag.UnitID == UnitID.Bullet)
            {
                switch (g2tag.UnitID)
                {
                     
                    default:
                        if (_isRemoveOnContact)
                            _components.RemoveUnit(this.ID, false);
                        else
                        {
                            if (_numberOfBounces > 0)
                            {
                                UpdateMovementDirection();
                                --_numberOfBounces;
                                base.Move();
                            }
                            else
                                _components.RemoveUnit(this.ID, false);
                        }
                        _components.particleEffects.TriggerEmitter("explosion", contactlist[0].Position);
                        return true;
                }
            }
            else
                return true;
        }

        public override void Create(object genericUnit, ComponentM components, Vector2 location, Vector2 direction, bool addtophysics)
        {
            base.Create(genericUnit, components, location, direction, addtophysics);
            if (_bulletManager == null)
                _bulletManager = components.bulletManager2;
            if(ObjectOnCollision == null)
                ObjectOnCollision += this.OnCollision;
            Bullet bullet = (Bullet)genericUnit;
            _numberOfBounces = bullet._numberOfBounces;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="startLocation"></param>
        /// <param name="endLocation"></param>
        public void Fire(Vector2 startLocation, Vector2 endLocation)
        {
            this.Position = startLocation;
            this.MoveDirection = Vector2.Subtract(startLocation, endLocation);
            this.Move();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gametime"></param>
        public override void Update(GameTime gametime)
        {
            base.Update(gametime);
        }
    }
}
